package ngames.com.sushitv.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.temp.Adapter.MyBannerAdapter;
import ngames.com.sushitv.temp.Cache.BitmapCache;
import ngames.com.sushitv.temp.Class.AnimeSeries;
import ngames.com.sushitv.temp.Interface.AnimeSeriesInterface;
import ngames.com.sushitv.temp.Interface.AnimeSeriesPresenter;
import ngames.com.sushitv.temp.Module.AnimeSeriesFactory;
import ngames.com.sushitv.temp.Thread.AnimeSeriesBannerThread;
import ngames.com.sushitv.temp.model.RankData;

/**
 * Created by edmond on 16-7-26.
 */
public class AnimeSeriesFragment extends Fragment implements AnimeSeriesInterface {
    private ImageView timg1, timg2, timg3, timg4, timg5, timg6;
    private TextView tplay1, tplay2, tplay3, tplay4, tplay5, tplay6;
    private TextView tdanmu1, tdanmu2, tdanmu3, tdanmu4, tdanmu5, tdanmu6;
    private TextView ttitle1, ttitle2, ttitle3, ttitle4, ttitle5, ttitle6;

    private ImageView nimg1, nimg2, nimg3, nimg4, nimg5, nimg6;
    private TextView nplay1, nplay2, nplay3, nplay4, nplay5, nplay6;
    private TextView ndanmu1, ndanmu2, ndanmu3, ndanmu4, ndanmu5, ndanmu6;
    private TextView ntitle1, ntitle2, ntitle3, ntitle4, ntitle5, ntitle6;

    private ImageView pimg1, pimg2, pimg3, pimg4, pimg5, pimg6;
    private TextView pplay1, pplay2, pplay3, pplay4, pplay5, pplay6;
    private TextView ptitle1, ptitle2, ptitle3, ptitle4, ptitle5, ptitle6;
    private View view,mView1,mView2,mView3;

    private ViewPager viewPager;
    private SwipeRefreshLayout swipeRefreshLayout;

    private List<ImageView> list;
    private LinearLayout linearLayout;

    private List<String> imageUrls = new ArrayList<>();

    private MyBannerAdapter adapter;
    private BannerListener listener;

    private int pointIndex = 0;

    private AnimeSeriesPresenter animeSeriesPresenter;

    private AppApplication app = null;

    private AnimeSeriesBannerThread thread;

    private Activity activity;

    private boolean isInit = false;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_anime_series, container, false);
        AppApplication.setContext(getContext());
        activity = getActivity();

        Log.d("FeaturedFragment","onCreate");

        Log.d("Fea","sss");

        AnimeSeriesFactory animeSeriesFractory = new AnimeSeriesFactory();
        animeSeriesPresenter = new AnimeSeriesPresenter(animeSeriesFractory,this);

        app = (AppApplication) getActivity().getApplication();
        if(app.queue==null){
            app.queue = Volley.newRequestQueue(getActivity());
        }

        if(app.imageLoader==null){
            app.imageLoader = new ImageLoader(app.queue,new BitmapCache());
        }

        findViews();
        initData();
        initAction();

        return view;
    }

    private void findViews(){
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        linearLayout = (LinearLayout) view.findViewById(R.id.points);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
    }

    private void initAction(){
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                animeSeriesPresenter.refreshData();
            }
        });
    }

    private void initData(){
        animeSeriesPresenter.initData();
    }

    private void setBanner(List<String> imageUrlsArray){
        if(isInit){
            viewPager.setOnPageChangeListener(null);
            linearLayout.removeAllViews();
            thread.setStop();
        }

        thread = new AnimeSeriesBannerThread(activity,viewPager,5000);

        imageUrls = imageUrlsArray;
        list = new ArrayList<>();
        View view;
        LinearLayout.LayoutParams params;

        Log.d("FeaturedFragment", String.valueOf(imageUrls.get(0)+"fdsf"));

        for(int i=0;i<imageUrls.size();i++){
            NetworkImageView imageView = new NetworkImageView(getActivity());
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            imageView.setDefaultImageResId(R.mipmap.ic_launcher);
            imageView.setErrorImageResId(R.mipmap.ic_launcher);
            Log.d("FeaturedFragment", String.valueOf(imageUrls.size()));
            imageView.setImageUrl(imageUrls.get(i),app.imageLoader);
            list.add(imageView);

            view = new View(getActivity());
            params = new LinearLayout.LayoutParams(20,20);
            params.leftMargin = 10;
            view.setBackgroundResource(R.drawable.point_background);
            view.setLayoutParams(params);
            view.setEnabled(false);

            linearLayout.addView(view);
        }

        adapter = new MyBannerAdapter(list);
        viewPager.setAdapter(adapter);
        listener = new BannerListener();

        viewPager.setOnPageChangeListener(listener);
        int index = (Integer.MAX_VALUE/2) - (Integer.MAX_VALUE/2%list.size());
        viewPager.setCurrentItem(1000);
        Log.d("Featured", String.valueOf(pointIndex));
        thread.start();
        Log.d("pointIndex", String.valueOf(pointIndex));
        linearLayout.getChildAt(pointIndex).setEnabled(true);
    }

    @Override
    public void initingData(List<String> imageUrls, List<AnimeSeries> featuredArray) {
        setBanner(imageUrls);
    }

    @Override
    public void initedData() {
        isInit = true;
    }

    @Override
    public void refresinghData() {

    }

    @Override
    public void refreshedData(List<String> imageUrls, List<AnimeSeries> featuredArray) {
        setBanner(imageUrls);
        Log.d("FeaturedFragment","FreshEd");
        swipeRefreshLayout.setRefreshing(false);
    }

    class BannerListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSelected(int position) {
            int newPosition = position % imageUrls.size();
            if(linearLayout.getChildAt(pointIndex)!=null){
                linearLayout.getChildAt(newPosition).setEnabled(true);
                linearLayout.getChildAt(pointIndex).setEnabled(false);
            }
            pointIndex = newPosition;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.d("FeaturedFragment","onPause");
        thread.setStop();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    /*********************************************/
    @Override
    public void addTop(List<RankData> rankDataList) {
        mView1 = view.findViewById(R.id.animeSeriesFragment_top);
        tplay1 = (TextView) mView1.findViewById(R.id.text_6p_play_count1);
        tplay2 = (TextView) mView1.findViewById(R.id.text_6p_play_count2);
        tplay3 = (TextView) mView1.findViewById(R.id.text_6p_play_count3);
        tplay4 = (TextView) mView1.findViewById(R.id.text_6p_play_count4);
        tplay5 = (TextView) mView1.findViewById(R.id.text_6p_play_count5);
        tplay6 = (TextView) mView1.findViewById(R.id.text_6p_play_count6);

        tdanmu1 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count1);
        tdanmu2 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count2);
        tdanmu3 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count3);
        tdanmu4 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count4);
        tdanmu5 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count5);
        tdanmu6 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count6);

        timg1 = (ImageView) mView1.findViewById(R.id.relative_6p_img1);
        timg2 = (ImageView) mView1.findViewById(R.id.relative_6p_img2);
        timg3 = (ImageView) mView1.findViewById(R.id.relative_6p_img3);
        timg4 = (ImageView) mView1.findViewById(R.id.relative_6p_img4);
        timg5 = (ImageView) mView1.findViewById(R.id.relative_6p_img5);
        timg6 = (ImageView) mView1.findViewById(R.id.relative_6p_img6);

        ttitle1 = (TextView) mView1.findViewById(R.id.text_6p_title1);
        ttitle2 = (TextView) mView1.findViewById(R.id.text_6p_title2);
        ttitle3 = (TextView) mView1.findViewById(R.id.text_6p_title3);
        ttitle4 = (TextView) mView1.findViewById(R.id.text_6p_title4);
        ttitle5 = (TextView) mView1.findViewById(R.id.text_6p_title5);
        ttitle6 = (TextView) mView1.findViewById(R.id.text_6p_title6);
        //List<RankData> rankDataList = JSON.parseArray(MyApplication.getTempStr(), RankData.class);
        TextView[] tplays = {tplay1, tplay2, tplay3, tplay4, tplay5, tplay6};
        TextView[] tdanmus = {tdanmu1, tdanmu2, tdanmu3, tdanmu4, tdanmu5, tdanmu6};
        ImageView[] timages = {timg1, timg2, timg3, timg4, timg5, timg6};
        TextView[] ttitles = {ttitle1, ttitle2, ttitle3, ttitle4, ttitle5, ttitle6};
        for (int i = 0; i < 6; ++i) {
            RankData rank = rankDataList.get(i);
            tplays[i].setText(rank.getPlay_count());
            tdanmus[i].setText(rank.getDanmu_count());
            ttitles[i].setText(rank.getTitle());
            animeSeriesPresenter.initImage(timages[i], rank.getCover());
            //HttpUtil_featured.setImage(timages[i], rank.getCover(), getContext());

        }
    }

    @Override
    public void addNewOngoingAnime(List<RankData> rankDataList) {
        mView2 = view.findViewById(R.id.animeSeriesFragment_newOngoingAnime);
        nplay1 = (TextView) mView2.findViewById(R.id.text_6p_play_count1);
        nplay2 = (TextView) mView2.findViewById(R.id.text_6p_play_count2);
        nplay3 = (TextView) mView2.findViewById(R.id.text_6p_play_count3);
        nplay4 = (TextView) mView2.findViewById(R.id.text_6p_play_count4);
        nplay5 = (TextView) mView2.findViewById(R.id.text_6p_play_count5);
        nplay6 = (TextView) mView2.findViewById(R.id.text_6p_play_count6);

        ndanmu1 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count1);
        ndanmu2 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count2);
        ndanmu3 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count3);
        ndanmu4 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count4);
        ndanmu5 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count5);
        ndanmu6 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count6);

        nimg1 = (ImageView) mView2.findViewById(R.id.relative_6p_img1);
        nimg2 = (ImageView) mView2.findViewById(R.id.relative_6p_img2);
        nimg3 = (ImageView) mView2.findViewById(R.id.relative_6p_img3);
        nimg4 = (ImageView) mView2.findViewById(R.id.relative_6p_img4);
        nimg5 = (ImageView) mView2.findViewById(R.id.relative_6p_img5);
        nimg6 = (ImageView) mView2.findViewById(R.id.relative_6p_img6);

        ntitle1 = (TextView) mView2.findViewById(R.id.text_6p_title1);
        ntitle2 = (TextView) mView2.findViewById(R.id.text_6p_title2);
        ntitle3 = (TextView) mView2.findViewById(R.id.text_6p_title3);
        ntitle4 = (TextView) mView2.findViewById(R.id.text_6p_title4);
        ntitle5 = (TextView) mView2.findViewById(R.id.text_6p_title5);
        ntitle6 = (TextView) mView2.findViewById(R.id.text_6p_title6);
        TextView[] nplays = {nplay1, nplay2, nplay3, nplay4, nplay5, nplay6};
        TextView[] ndanmus = {ndanmu1, ndanmu2, ndanmu3, ndanmu4, ndanmu5, ndanmu6};
        ImageView[] nimages = {nimg1, nimg2, nimg3, nimg4, nimg5, nimg6};
        TextView[] ntitles = {ntitle1,ntitle2, ntitle3, ntitle4, ntitle5, ntitle6};
        for (int i = 0; i < 6; ++i) {
            RankData rank = rankDataList.get(i);
            nplays[i].setText(rank.getPlay_count());
            ndanmus[i].setText(rank.getDanmu_count());
            ntitles[i].setText(rank.getTitle());
            animeSeriesPresenter.initImage(nimages[i], rank.getCover());
            //HttpUtil_featured.setImage(images[i], rank.getCover(), getContext());
        }
    }

    @Override
    public void addPopularCompletedAnime(List<RankData> rankDataList) {
        mView3 = view.findViewById(R.id.animeSeriesFragment_popularCompletedAnime);
        pplay1 = (TextView) mView3.findViewById(R.id.text_6p_num1);
        pplay2 = (TextView) mView3.findViewById(R.id.text_6p_num2);
        pplay3 = (TextView) mView3.findViewById(R.id.text_6p_num3);
        pplay4 = (TextView) mView3.findViewById(R.id.text_6p_num4);
        pplay5 = (TextView) mView3.findViewById(R.id.text_6p_num5);
        pplay6 = (TextView) mView3.findViewById(R.id.text_6p_num6);

        pimg1 = (ImageView) mView3.findViewById(R.id.image_6p_img1);
        pimg2 = (ImageView) mView3.findViewById(R.id.image_6p_img2);
        pimg3 = (ImageView) mView3.findViewById(R.id.image_6p_img3);
        pimg4 = (ImageView) mView3.findViewById(R.id.image_6p_img4);
        pimg5 = (ImageView) mView3.findViewById(R.id.image_6p_img5);
        pimg6 = (ImageView) mView3.findViewById(R.id.image_6p_img6);

        ptitle1 = (TextView) mView3.findViewById(R.id.text_6p_title1);
        ptitle2 = (TextView) mView3.findViewById(R.id.text_6p_title2);
        ptitle3 = (TextView) mView3.findViewById(R.id.text_6p_title3);
        ptitle4 = (TextView) mView3.findViewById(R.id.text_6p_title4);
        ptitle5 = (TextView) mView3.findViewById(R.id.text_6p_title5);
        ptitle6 = (TextView) mView3.findViewById(R.id.text_6p_title6);
        TextView[] pplays = {pplay1, pplay2, pplay3, pplay4, pplay5, pplay6};
        ImageView[] pimages = {pimg1, pimg2, pimg3, pimg4, pimg5, pimg6};
        TextView[] ptitles = {ptitle1, ptitle2, ptitle3, ptitle4, ptitle5, ptitle6};
        for (int i = 0; i < 6; ++i) {
            RankData rank = rankDataList.get(i);
            pplays[i].setText(rank.getLast_episode());
            ptitles[i].setText(rank.getTitle());
            animeSeriesPresenter.initImage(pimages[i], rank.getCover());
            //HttpUtil_featured.setImage(images[i], rank.getCover(), getContext());
        }
    }

    @Override
    public void addFeaturedAnime(List<RankData> datas) {

    }


}
